<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="(item,index) in list" :key="index" :title="item.content">
        <van-image
          slot="icon"
          round
          class="avater"
          :src="item.aut_photo"
        />
        <div slot="title">
        <div class="title-wrap">
         <span style="color: #466b9d;">{{item.aut_name}}</span>
         <div class="like-wrap">
             <van-icon :name="item.is_liking?'good-job':'good-job-o'" @click="onCommentLike(item)" />
           <span>{{item.like_count>0?item.like_count:''}}</span>
           </div>
           
        </div>
            <div class="content" style="color: #363636;">{{item.content}}</div>
          <div class="reply">
           <span style="margin-right: 10px;">{{item.pubdate|datetime}}</span>
           <van-button size="mini" round 
           @click="$emit('click-reply',item)"
           >{{item.reply_count}}回复</van-button>
          </div>
         
           
</div>
      </van-cell>
    </van-list>
    <!-- 评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field clearable placeholder="请输入评论内容">
        <van-button slot="button" size="mini" type="info">发布</van-button>
      </van-field>
    </van-cell-group>
    <!-- /发布评论 -->
  </div>
</template>

<script>
import { getComments,addCommentLike,deleteCommentLike } from '@/api/comment.js'
  export default {
    name: "ArticleComment",
    props: {
      articleId: {
         type: [Number, String,Object],
         required: true
        },
      list:{
           type:Array,
    //数组或者对象的默认值必须通过函数返回
        default:function(){
        return []
      }
     }
     },
    data() {
      return {
    
      //list: [],
      loading: false,
      finished: false,
      offset: null, // 请求下一页数据的页码
      totalCount: 0 // 总数据条数
      };
    },
  created(){
    this.onLoad();
  },
    methods: {
    async  onLoad() {
        // 异步更新数据
         const articleCommentM = this.articleCommentM
      // 1. 请求获取数据
     const { data } = await getComments({
     type: 'a', // 评论类型，a-对文章(article)的评论，c-对评论(comment)的回复
     source: this.articleId, // 源id，文章id或评论id
     offset: this.offset, // 获取评论数据的偏移量，值为评论id，表示从此id的数据向后取，不传表示从第一页开始读取数据
     limit: 10 // 每页大小
  })

  // 2. 将数据添加到列表中
  const { results } = data.data
  
  this.list.push(...results)

  // 更新总数据条数
  this.totalCount = data.data.total_count
  this.$emit('update-total-count', data.data.total_count)
  // 3. 将加载更多的 loading 设置为 false
  this.loading = false

  // 4. 判断是否还有数据
  if (results.length) {
    this.offset = data.data.last_id // 更新获取下一页数据的页码
  } else {
    this.finished = true // 没有数据了，关闭加载更多
  }
      }
      ,
      async onCommentLike (comment) {
  // 如果已经赞了则取消点赞
  if (comment.is_liking) {
    await deleteCommentLike(comment.com_id)
    comment.like_count--
  } else {
    // 如果没有赞，则点赞
    await addCommentLike(comment.com_id)
     comment.like_count++
  }

  // 更新视图状态
  comment.is_liking = !comment.is_liking
  this.$toast('操作成功')
}
    }
  };
</script>

<style scoped lang="less">
  .publish-wrap {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
  }
  .avater{
      width:30px;
      height:30px;
    margin-right: 10px;

  }

  .van-list {
    margin-bottom: 45px;
  }
  .title-wrap{
      display: flex;
      justify-content: space-between;
  }
  .reply{
      display: flex;
      align-items: center;
    //   justify-content: center;
  }
  .like-wrap{
      display: flex;
      align-items: center;
      justify-content: center;
  }
</style>